function [struct,tempStruct]=structReplace(struct,fieldToReplace,currentNames,newNames)
% =========================================================================
%% function
%% [struct,tempStruct]=structReplace(struct,fieldToReplace,currentNames,newNames)
%
%% Inputs 
%  struct           Structure 
%                   Single or multiple 
%
%  fieldToReplace   String with the name of the field or sub-structure in struct to replace 
%                   if none
%  currentNames     Cell with the strings in struct.fieldToReplace 
%                   that must be replaced 
%  newNames         Cell with the new names 
%
%% Output 
%  struct           Original structure with the field replaced as above 
%  tempStruct       New structure with a single field equal to
%                   fieldToReplace
% 
% Alejandro Justiniano  Dec 13 2012 
% =========================================================================
if isstruct(struct)==false; 
    error('struct must be a structure') 
end 
if isfield(struct,char(fieldToReplace))==false; 
    dispaj('In struct, field ',fieldToReplace,' does not exist'); 
    error('Non existent field'); 
end 
fldnm=fieldToReplace; 
%% Position in the original cell of the names to replace
pos=cellposition(currentNames,struct.(fldnm)); 
struct.(fldnm)(pos)=newNames; 
tempStruct.(fldnm)=struct.(fldnm); 